package com.chexiehui.jc.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aries.ui.view.radius.RadiusCheckBox;
import com.aries.ui.view.radius.RadiusRadioButton;
import com.aries.ui.view.radius.RadiusTextView;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chexiehui.jc.AppApplication;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpActivity;
import com.chexiehui.jc.base.BasePage;
import com.chexiehui.jc.entity.BaiDuLocationInfo;
import com.chexiehui.jc.entity.CarInfo;
import com.chexiehui.jc.entity.CarSeriesInfo;
import com.chexiehui.jc.entity.ShopInfo;
import com.chexiehui.jc.entity.TelInfo;
import com.chexiehui.jc.event.CarCompareRecodeDeleteEvent;
import com.chexiehui.jc.mvp.car.CarModelDetailsCovenant;
import com.chexiehui.jc.mvp.car.CarModelDetailsPresenter;
import com.chexiehui.jc.utils.BundleBuilder;
import com.chexiehui.jc.utils.GlideUtil;
import com.chexiehui.jc.utils.NoDoubleClickListener;
import com.chexiehui.jc.utils.NoDoubleClickUtils;
import com.chexiehui.jc.utils.SoftKeyboardFixerForFullscreen;
import com.chexiehui.jc.widget.CustomInquiryPopup;
import com.chexiehui.jc.widget.CustomStageBuyCarPopup;
import com.chexiehui.jc.widget.LoadingLayout;
import com.lxj.xpopup.XPopup;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.PermissionListener;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 车型详情
 */
public class CarModelDetailsActivity extends BaseMvpActivity<CarModelDetailsPresenter> implements
        CarModelDetailsCovenant.View, CustomStageBuyCarPopup.ICustomStageBuyCarPopupListener, CustomInquiryPopup.ICustomInquiryPopupListener {

    @BindView(R.id.rtv_choose_compare_count)
    RadiusTextView rtv_choose_compare_count;
    @BindView(R.id.iv_car_image)
    ImageView ivCarImage;
    @BindView(R.id.tv_car_name)
    TextView tvCarName;
    @BindView(R.id.tv_car_price)
    TextView tvCarPrice;
    @BindView(R.id.tv_parameter_config)
    TextView tvParameterConfig;
    @BindView(R.id.tv_car_forum)
    TextView tvCarForum;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.rrb_desc_type_0)
    RadiusRadioButton rrbDescType0;
    @BindView(R.id.rrb_desc_type_1)
    RadiusRadioButton rrbDescType1;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.loadingLayout2)
    LoadingLayout loadingLayout2;
    @BindView(R.id.tv_model_compare)
    TextView tvModelCompare;
    @BindView(R.id.tv_contact_sales)
    TextView tvContactSales;
    @BindView(R.id.tv_stages_buy)
    TextView tvStagesBuy;
    @BindView(R.id.rtv_goto_inquiry)
    RadiusTextView rtvGotoInquiry;
    @BindView(R.id.loadingLayout)
    LoadingLayout loadingLayout;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private CarSeriesInfo seriesInfo;
    private CarInfo carModel;
    private BaiDuLocationInfo nowCity;

    private CustomInquiryPopup customInquiryPopup;
    private CustomStageBuyCarPopup customStageBuyCarPopup;
    private String salespersonTel;
    private int descType;

    private CommonAdapter<ShopInfo> shopListAdapter;
    private BasePage<ShopInfo> page;

    @Override
    protected void beforeSetView() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            seriesInfo = (CarSeriesInfo) bundle.getSerializable("seriesInfo");
            carModel = (CarInfo) bundle.getSerializable("carModel");
        }
        if(seriesInfo == null) seriesInfo = new CarSeriesInfo();
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void setTitleBar() {
        titleBar.setTitleMainText("车型详情");
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_car_model_details;
    }

    @Override
    protected void initView(Bundle var1) {
        if(carModel == null){
            loadingLayout.setStatus(LoadingLayout.Empty);
            return;
        }
        SoftKeyboardFixerForFullscreen.assistActivity(this);
        loadingLayout.setStatus(LoadingLayout.Success);
        loadingLayout2.setStatus(LoadingLayout.Loading);
        loadingLayout2.setOnReloadListener(v -> {
            if(refreshLayout.getState() == RefreshState.Refreshing ||
                    refreshLayout.getState() == RefreshState.Loading){
                return;
            }
            mvpPresenter.getShopList(1);
        });
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setDisableContentWhenLoading(true);
        refreshLayout.setDisableContentWhenRefresh(true);
        refreshLayout.setEnableFooterFollowWhenLoadFinished(false);
        refreshLayout.setEnableLoadMoreWhenContentNotFull(false);
        refreshLayout.setEnableScrollContentWhenRefreshed(true);
        recycler.setLayoutManager(new LinearLayoutManager(mContext));
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (loadingLayout2.getStatus() == LoadingLayout.Loading) {
                    refreshLayout.finishRefresh(true);
                    return;
                }
                mvpPresenter.getShopList(1);
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (loadingLayout2.getStatus() == LoadingLayout.Loading) {
                    refreshLayout.finishLoadMore(true);
                    return;
                }
                if (page != null && page.getTotalRecordCount() > shopListAdapter.getDatas().size()) {
                    mvpPresenter.getShopList(page.getPageIndex() + 1);
                } else {
                    refreshLayout.finishLoadMoreWithNoMoreData();
                }
            }
        });
        shopListAdapter = new CommonAdapter<ShopInfo>(mContext, R.layout.item_home_slide_4s_shop, new ArrayList<>()) {
            @Override
            protected void convert(ViewHolder holder, ShopInfo item, int position) {
                holder.setText(R.id.tv_item_name, item.getName());
                holder.setText(R.id.tv_item_address, item.getAdress());
                holder.setText(R.id.tv_item_brand, "主营品牌：" + item.getBrandName());
                holder.setText(R.id.tv_item_distance, "距离" + item.getDistanceStr());
                TextView tv_item_other_brand = holder.getView(R.id.tv_item_other_brand);
                if(StringUtils.isTrimEmpty(item.getOtherBrand())){
                    tv_item_other_brand.setVisibility(View.GONE);
                }else{
                    tv_item_other_brand.setVisibility(View.VISIBLE);
                    tv_item_other_brand.setText("其他品牌：" + item.getOtherBrand());
                }
                holder.getView(R.id.rtv_item_consult).setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View view) {
                        if (StringUtils.isTrimEmpty(item.getTel())) {
                            showToast("暂无联系方式");
                        } else {
                            callPhone(item.getTel());
                        }
                    }
                });
            }
        };
        shopListAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                startActivity(ShopDetailsActivity.class,
                        new BundleBuilder()
                                .putString("id", shopListAdapter.getDatas().get(position).getId())
                                .create());
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        recycler.setAdapter(shopListAdapter);
        changeDescType(0);
        //mvpPresenter.getSeriesInfo();
        setHeadInfo();
        mvpPresenter.getSalespersonTel();
    }

    private void setShowCompareCount() {
        long count = AppApplication.getInstances().getCarInfoDao().queryBuilder().count();
        rtv_choose_compare_count.setText(count + "");
        rtv_choose_compare_count.setVisibility(count > 0 ? View.VISIBLE : View.GONE);
    }

    private void setHeadInfo() {
        GlideUtil.loadImageViewCrop(mContext, carModel.getImg(), ivCarImage, R.drawable.shape_square, R.drawable.shape_square);
        tvCarName.setText(carModel.getShowTitle());
        tvCarPrice.setText("指导价：" + carModel.getNewPriceStrUnit());
        tvLocation.setText("正在定位");
        rtv_choose_compare_count.setVisibility(View.GONE);
        setShowCompareCount();
        requestAppPermissions();
    }

    private void requestAppPermissions() {
        AndPermission.with(this)
                .permission(Permission.LOCATION, Permission.STORAGE)
                .callback(new PermissionListener() {
                    @Override
                    public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                        if (!AppApplication.getInstances().getLocationService().isStart()) {
                            AppApplication.getInstances().getLocationService().start();
                        }
                        AppApplication.getInstances().getLocationService().registerListener(mListener);
                    }

                    @Override
                    public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                        if (AndPermission.hasAlwaysDeniedPermission(mContext, deniedPermissions)) {
                            AndPermission.defaultSettingDialog(mContext, Constants.REQUEST_CODE_LOACTION).show();
                        } else {
                            showToast(getStringSource(R.string.app_name) + "需要您授权位置服务和访问文件的权限");
                        }
                    }
                })
                .requestCode(Constants.REQUEST_CODE_CARAME)
                .rationale((requestCode, rationale) -> AndPermission.rationaleDialog(mContext, rationale).show()).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                BDLocation location = (BDLocation) msg.obj;
                nowCity = new BaiDuLocationInfo(location.getCity(), location.getLatitude(), location.getLongitude());
                tvLocation.setText(location.getCity());
                loadingLayout2.setStatus(LoadingLayout.Loading);
                mvpPresenter.getShopList(1);
            }
        }
    };

    private BDAbstractLocationListener mListener = new BDAbstractLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (null != location && location.getLocType() != BDLocation.TypeServerError) {
                if (!StringUtils.isTrimEmpty(location.getCity())) {
                    AppApplication.getInstances().getLocationService().unregisterListener(mListener);
                    Message msg = new Message();
                    msg.obj = location;
                    msg.what = 0;
                    handler.sendMessage(msg);
                }
            }
        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {
            super.onConnectHotSpotMessage(s, i);
        }

        @Override
        public void onLocDiagnosticMessage(int locType, int diagnosticType, String diagnosticMessage) {
            super.onLocDiagnosticMessage(locType, diagnosticType, diagnosticMessage);
            LogUtils.e("定位失败");
        }
    };

    @Override
    protected void onDestroy() {
        handler.removeCallbacksAndMessages(null);
        handler = null;
        AppApplication.getInstances().getLocationService().unregisterListener(mListener);
        mListener = null;
        super.onDestroy();
    }


    @OnClick({R.id.tv_location, R.id.tv_parameter_config,
            R.id.tv_car_forum, R.id.tv_model_compare, R.id.tv_contact_sales, R.id.tv_stages_buy,
            R.id.rtv_goto_inquiry})
    public void onViewClicked(View view) {
        if (NoDoubleClickUtils.isDoubleClick()) return;
        switch (view.getId()) {
            case R.id.tv_location:
                startActivityForResult(ChooseCityActivity.class, 300);
                break;
            case R.id.tv_parameter_config:
                ArrayList<CarInfo> carInfos = new ArrayList<>();
                carInfos.add(carModel);
                startActivity(CarBrandParameterActivity.class,
                        new BundleBuilder()
                                .putSerializable("compareList", carInfos)
                                .create());
                break;
            case R.id.tv_car_forum:
                if (StringUtils.isTrimEmpty(seriesInfo.getForumId())) {
                    showToast("暂无论坛");
                } else {
                    startActivity(ForumTribuneDetailsActivity.class, new BundleBuilder()
                            .putString("forumId", seriesInfo.getForumId())
                            .create());
                }
                break;
            case R.id.tv_model_compare:
                startActivity(CarModelCompareActivity.class);
                break;
            case R.id.tv_contact_sales:
                if (StringUtils.isTrimEmpty(salespersonTel)) {
                    showToast("暂无销售电话");
                } else {
                    callPhone(salespersonTel);
                }
                break;
            case R.id.tv_stages_buy:
                showStageBuyCarPopup(carModel);
                break;
            case R.id.rtv_goto_inquiry:
                showInquiryPopup(carModel);
                break;
        }
    }

    private void callPhone(String phone) {
        AndPermission.with(this)
                .permission(Permission.PHONE)
                .callback(new PermissionListener() {
                    @Override
                    public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                        PhoneUtils.call(phone);
                    }

                    @Override
                    public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                        if (AndPermission.hasAlwaysDeniedPermission(mContext, deniedPermissions)) {
                            AndPermission.defaultSettingDialog(mContext, Constants.REQUEST_CODE_LOACTION).show();
                        } else {
                            showToast(getStringSource(R.string.app_name) + "需要您授权电话权限");
                        }
                    }
                })
                .requestCode(Constants.REQUEST_CODE_CARAME)
                .rationale((requestCode, rationale) -> AndPermission.rationaleDialog(mContext, rationale).show()).start();
    }

    private void showInquiryPopup(CarInfo carInfo) {
        if (customInquiryPopup == null) {
            customInquiryPopup = new CustomInquiryPopup(mContext);
            customInquiryPopup.setListener(this);
        }
        customInquiryPopup.initCarModel(carInfo);
        if (nowCity != null) {
            customInquiryPopup.initCity(nowCity);
        }
        new XPopup.Builder(mContext)
                .autoOpenSoftInput(false)
                .autoFocusEditText(false)
                .asCustom(customInquiryPopup)
                .show();
    }

    private void showStageBuyCarPopup(CarInfo carInfo) {
        if (customStageBuyCarPopup == null) {
            customStageBuyCarPopup = new CustomStageBuyCarPopup(mContext);
            customStageBuyCarPopup.setListener(this);
        }
        customStageBuyCarPopup.initCarModel(carInfo);
        if (nowCity != null) {
            customStageBuyCarPopup.initCity(nowCity);
        }
        new XPopup.Builder(mContext)
                .autoOpenSoftInput(false)
                .autoFocusEditText(false)
                .asCustom(customStageBuyCarPopup)
                .show();
    }

    @Override
    protected CarModelDetailsPresenter createPresenter() {
        return new CarModelDetailsPresenter(this);
    }

   /* @Override
    public void onGetSeriesInfoSuccess(BaseModel<CarSeriesInfo> successResult) {
        if (loadingLayout.getStatus() == LoadingLayout.Loading) {
            loadingLayout.setStatus(LoadingLayout.Success);
        }
        seriesInfo = successResult.getData();

    }

    @Override
    public void onGetSeriesInfoFailure(BaseModel<Object> failureResult) {
        if (failureResult.getResultCode() == Constants.PAGE_DATA_EMPTY) {
            loadingLayout.setStatus(LoadingLayout.Empty);
            loadingLayout.setEmptyText(failureResult.getResultInfo());
        } else {
            loadingLayout.setStatus(LoadingLayout.Error);
            loadingLayout.setErrorText(failureResult.getResultInfo());
        }
    }*/

    @Override
    public void onAddInstallmentSuccess(BaseModel<Object> successResult) {
        showLongToast("申请已提交，销售顾问会尽快与您联系！");
        customStageBuyCarPopup.dismiss();
        customStageBuyCarPopup.releaseObj();
        customStageBuyCarPopup = null;
    }

    @Override
    public void onAddInstallmentFailure(BaseModel<Object> failureResult) {
        showToast(failureResult.getResultInfo());
    }

    @Override
    public void onAddShopInquirySuccess(BaseModel<Object> successResult) {
        showLongToast("申请已提交，销售顾问会尽快与您联系！");
        customInquiryPopup.dismiss();
        customInquiryPopup.releaseObj();
        customInquiryPopup = null;
    }

    @Override
    public void onAddShopInquiryFailure(BaseModel<Object> failureResult) {
        showToast(failureResult.getResultInfo());
    }

    @Override
    public void onGetSalespersonTelSuccess(BaseModel<TelInfo> successResult) {
        if (successResult.getData() != null) {
            salespersonTel = successResult.getData().getTel();
        }

    }

    @Override
    public void onGetSalespersonTelFailure(BaseModel<Object> failureResult) {

    }

    @Override
    public void onGetShopListSuccess(BaseModel<BasePage<ShopInfo>> successResult) {
        page = successResult.getData();
        if (loadingLayout2.getStatus() != LoadingLayout.Success) {
            loadingLayout2.setStatus(LoadingLayout.Success);
        }
        if (refreshLayout.getState() == RefreshState.Refreshing) {
            refreshLayout.finishRefresh();
            shopListAdapter.getDatas().clear();
            shopListAdapter.getDatas().addAll(page.getItems());
            shopListAdapter.notifyDataSetChanged();
        } else if (refreshLayout.getState() == RefreshState.Loading) {
            shopListAdapter.getDatas().addAll(page.getItems());
            shopListAdapter.notifyDataSetChanged();
            if (page.getTotalRecordCount() <= shopListAdapter.getDatas().size()) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore(true);
            }
        } else {
            refreshLayout.finishRefresh();
            shopListAdapter.getDatas().clear();
            shopListAdapter.getDatas().addAll(page.getItems());
            shopListAdapter.notifyDataSetChanged();
        }
        if(loadingLayout2.getStatus() == LoadingLayout.Loading){
            loadingLayout2.setStatus(LoadingLayout.Success);
        }
        if (refreshLayout.getState() == RefreshState.Refreshing) {
            refreshLayout.finishRefresh(true);
        }
    }

    @Override
    public void onGetShopListFailure(BaseModel<Object> failureResult) {
        if (Constants.PAGE_DATA_EMPTY == failureResult.getResultCode()) {
            if (refreshLayout.getState() == RefreshState.Refreshing) {
                refreshLayout.finishRefresh(true);
                if (shopListAdapter.getDatas().size() > 0) {
                    shopListAdapter.getDatas().clear();
                    shopListAdapter.notifyDataSetChanged();
                }
                loadingLayout2.setStatus(LoadingLayout.Empty);
                loadingLayout2.setEmptyText(failureResult.getResultInfo());
            } else if (refreshLayout.getState() == RefreshState.Loading) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                loadingLayout2.setStatus(LoadingLayout.Empty);
                loadingLayout2.setEmptyText(failureResult.getResultInfo());
            }
        } else {
            if (refreshLayout.getState() == RefreshState.Refreshing) {
                refreshLayout.finishRefresh(false);
            }
            if (refreshLayout.getState() == RefreshState.Loading) {
                refreshLayout.finishLoadMore(false);
            }
            if (shopListAdapter.getDatas().size() <= 0) {
                loadingLayout2.setStatus(LoadingLayout.Error);
                loadingLayout2.setErrorText(failureResult.getResultInfo());
            } else {
                showToast(failureResult.getResultInfo());
            }
        }
    }

    @Override
    public String getSeriesId() {
        return seriesInfo.getId();
    }

    @Override
    public String getLocationLat() {
        return nowCity != null ? nowCity.getLat() + "" : "";
    }

    @Override
    public String getLocationLng() {
        return nowCity != null ? nowCity.getLng() + "" : "";
    }

    @Override
    public String getLocationCity() {
        return nowCity == null || nowCity.getCity() == null ? "" : nowCity.getCity();
    }

    @Override
    public String getCarId() {
        return carModel == null ? "" : carModel.getId();
    }

    @Override
    public String getDescType() {
        return descType + "";
    }

    private void changeDescType(int index) {
        if (index == descType) {
            return;
        }
        descType = index;
        if (descType == 0) {
            rrbDescType0.setSelected(true);
            rrbDescType1.setSelected(false);
        } else {
            rrbDescType0.setSelected(false);
            rrbDescType1.setSelected(true);
        }
    }

    @OnClick({R.id.rrb_desc_type_0, R.id.rrb_desc_type_1})
    public void onViewClicked2(View view) {
        switch (view.getId()) {
            case R.id.rrb_desc_type_0:
                if (descType != 0) {
                    changeDescType(0);
                    loadingLayout2.setStatus(LoadingLayout.Loading);
                    mvpPresenter.getShopList(1);
                }
                break;
            case R.id.rrb_desc_type_1:
                if (descType != 1) {
                    changeDescType(1);
                    loadingLayout2.setStatus(LoadingLayout.Loading);
                    mvpPresenter.getShopList(1);
                }
                break;
        }
    }

    @Override
    public void onStageBuyCarPopupCallChooseModel() {
        startActivityForResult(ChooseCarModelActivity.class, 10,
                new BundleBuilder().putSerializable("seriesInfo", seriesInfo).create());
    }

    @Override
    public void onStageBuyCarPopupCallChooseCity() {
        startActivityForResult(ChooseCityActivity.class, 11);
    }

    @Override
    public void onStageBuyCarPopupCallChooseMerchant() {
        if (customStageBuyCarPopup != null) {
            if (customStageBuyCarPopup.getChooseModel() == null) {
                showToast("请选择车型");
                return;
            }
            if (customStageBuyCarPopup.getChooseCity() == null) {
                showToast("请选择城市");
                return;
            }
            startActivityForResult(Shop4sChooseActivity.class, 12,
                    new BundleBuilder()
                            .putSerializable("carId", getSeriesId())
                            .putSerializable("location", customStageBuyCarPopup.getChooseCity())
                            .create());
        }
    }

    @Override
    public void onStageBuyCarPopupApplySubmit(String carid, String shopids, String phone, String name, String city) {
        mvpPresenter.addInstallment(carid, shopids, phone, name, city);
    }

    @Override
    public void onStageBuyCarPopupLookAgreement() {
        startActivity(WebActivity.class, new BundleBuilder().putString("url", Constants.YRL_CYHGRXXBHTK).create());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 10:
                if (resultCode == RESULT_OK) {
                    CarInfo carInfo = (CarInfo) data.getSerializableExtra("carInfo");
                    if (carInfo != null && customStageBuyCarPopup != null) {
                        customStageBuyCarPopup.setChooseModel(carInfo);
                    }
                }
                break;
            case 11:
                if (resultCode == RESULT_OK) {
                    if (data != null) {
                        BaiDuLocationInfo locationInfo = (BaiDuLocationInfo) data.getSerializableExtra("entity");
                        if (locationInfo != null && !StringUtils.isTrimEmpty(locationInfo.getCity()) && customStageBuyCarPopup != null) {
                            customStageBuyCarPopup.setChooseCity(locationInfo);
                        }
                    }
                }
                break;
            case 12:
                if (resultCode == RESULT_OK) {
                    if (data != null) {
                        ArrayList<String> ids = data.getStringArrayListExtra("chooseList");
                        if (ids != null && ids.size() > 0 && customStageBuyCarPopup != null) {
                            customStageBuyCarPopup.setChooseMerchant(ids);
                        }
                    }
                }
                break;

            case 20:
                if (resultCode == RESULT_OK) {
                    CarInfo carInfo = (CarInfo) data.getSerializableExtra("carInfo");
                    if (carInfo != null && customInquiryPopup != null) {
                        customInquiryPopup.setChooseModel(carInfo);
                    }
                }
                break;
            case 21:
                if (resultCode == RESULT_OK) {
                    if (data != null) {
                        BaiDuLocationInfo locationInfo = (BaiDuLocationInfo) data.getSerializableExtra("entity");
                        if (locationInfo != null && !StringUtils.isTrimEmpty(locationInfo.getCity()) && customInquiryPopup != null) {
                            customInquiryPopup.setChooseCity(locationInfo);
                        }
                    }
                }
                break;
            case 22:
                if (resultCode == RESULT_OK) {
                    if (data != null) {
                        ArrayList<String> ids = data.getStringArrayListExtra("chooseList");
                        if (ids != null && ids.size() > 0 && customInquiryPopup != null) {
                            customInquiryPopup.setChooseMerchant(ids);
                        }
                    }
                }
                break;
            case 300:
                if (resultCode == RESULT_OK) {
                    if (data != null) {
                        BaiDuLocationInfo locationInfo = (BaiDuLocationInfo) data.getSerializableExtra("entity");
                        if (locationInfo != null && !StringUtils.isTrimEmpty(locationInfo.getCity())) {
                            if(nowCity == null){
                                nowCity = locationInfo;
                            }else{
                                nowCity.setCity(locationInfo.getCity());
                            }
                            tvLocation.setText(nowCity.getCity());
                            loadingLayout2.setStatus(LoadingLayout.Loading);
                            mvpPresenter.getShopList(1);
                        }
                    }
                }
                break;
        }
    }

    @Override
    public void onInquiryPopupCallChooseModel() {
        startActivityForResult(ChooseCarModelActivity.class, 20,
                new BundleBuilder().putSerializable("seriesInfo", seriesInfo).create());
    }

    @Override
    public void onInquiryPopupCallChooseCity() {
        startActivityForResult(ChooseCityActivity.class, 21);
    }

    @Override
    public void onInquiryPopupCallChooseMerchant() {
        if (customInquiryPopup != null) {
            if (customInquiryPopup.getChooseModel() == null) {
                showToast("请选择车型");
                return;
            }
            if (customInquiryPopup.getChooseCity() == null) {
                showToast("请选择城市");
                return;
            }
            startActivityForResult(Shop4sChooseActivity.class, 22,
                    new BundleBuilder()
                            .putSerializable("carId", getSeriesId())
                            .putSerializable("location", customInquiryPopup.getChooseCity())
                            .create());
        }
    }

    @Override
    public void onInquiryPopupApplySubmit(String carid, String shopids, String phone, String name, String city) {
        mvpPresenter.addShopInquiry(carid, shopids, phone, name, city);
    }

    @Override
    public void onInquiryPopupLookAgreement() {
        startActivity(WebActivity.class, new BundleBuilder().putString("url", Constants.YRL_CYHGRXXBHTK).create());
    }

    @Subscribe
    public void onCarCompareRecodeDeleteEvent(CarCompareRecodeDeleteEvent event) {
        setShowCompareCount();
    }

}
